﻿#region

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using InfoTech.VinhSon.Bussiness;
using InfoTech.VinhSon.Models;
using Telerik.Web.Mvc;

#endregion

namespace VinhSon.Areas.Administrator.Controllers
{
    public class LayoutController : BaseAdministratorController
    {
        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GetList()
        {
            return View(new GridModel(UnitOfWork.LayoutRepository.Select(c => c.Inactive == false || c.Inactive == null)
                                                .Select(c => new
                                                                 {
                                                                     c.Id,
                                                                     c.Name,
                                                                     c.Area,
                                                                     c.Controller,
                                                                     c.Action,
                                                                     c.Partial
                                                                 })));
        }

        public ActionResult Add()
        {
            return View(new LayoutModel());
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Add(LayoutModel model)
        {
            if (ModelState.IsValid)
            {
                Layout layout = model;
                if (UnitOfWork.LayoutRepository.CheckInsertLayout(layout))
                {
                    UnitOfWork.LayoutRepository.Insert(layout);
                    UnitOfWork.Save();
                    return RedirectToAction("Index");
                }
            }
            return View(model);
        }

        public ActionResult Edit(string id)
        {
            int guid;
            if (int.TryParse(id,
                            out guid))
            {
                Layout layout = UnitOfWork.LayoutRepository.SelectbyId(guid);
                if (layout != null)
                {
                    LayoutModel webLayoutModel = layout;
                    return View(webLayoutModel);
                }
            }
            return RedirectToAction("Index");
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(LayoutModel model)
        {
            if (ModelState.IsValid)
            {
                Layout layout = UnitOfWork.LayoutRepository.SelectbyId(model.Id);
                model.Update(layout);
                if (UnitOfWork.LayoutRepository.CheckUpdateLayout(layout))
                {
                    UnitOfWork.LayoutRepository.Update(layout);
                    UnitOfWork.Save();
                    return RedirectToAction("Index");
                }
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            if (id != null)
            {
                string[] s = id.Split(',');
                int guid;
                List<int> list = (from s1 in s
                                  where int.TryParse(s1,
                                                     out guid)
                                  select int.Parse(s1)).ToList();

                if (UnitOfWork.LayoutRepository.CheckDeleteLayout(list))
                {
                    UnitOfWork.LayoutRepository.Delete(list);
                    UnitOfWork.Save();
                }
            }
            return Json(new
                            {
                                status = 1,
                                msg = "Update Successfully"
                            },
                        JsonRequestBehavior.DenyGet);
        }
    }
}
